1. 锐化空间滤波

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

// 定义滤波函数
void myfilter(Mat& img_input,Mat& img_output); // 四邻域
void myfilter2(Mat& img_input,Mat& img_output); // 八邻域

int main(){
Mat img,img_gray,imt_output,img_output2;
img = imread("/home/v/home.png");
if (img.empty()){
cout<<"can not load image"<<endl;
return -1;
}
imshow("Img",img);
cvtColor(img,img_gray,COLOR_BGR2GRAY);
imshow("img gray",img_gray);

myfilter(img_gray,imt_output); // 四领域
imshow("img output",imt_output);

myfilter2(img_gray,img_output2); // 八领域
imshow("img output2",img_output2);

waitKey(0);
return 0;
}

// 四领域
void myfilter(Mat& img_input,Mat& img_output){
img_output = img_input.clone();
int la;

for (int i=1;i<img_input.rows-1;i++){
for (int j=1;j<img_input.cols-1;j++){
la = 4*img_input.at<uchar>(i,j)-img_input.at<uchar>(i-1,j)-img_input.at<uchar>(i+1,j)-
img_input.at<uchar>(i,j-1)-img_input.at<uchar>(i,j+1);
img_output.at<uchar>(i,j) = saturate_cast<uchar>(la+img_output.at<uchar>(i,j));
}
}
}

// 八领域
void myfilter2(Mat& img_input,Mat& img_output){

img_output = img_input.clone();
int la2;
for (int i=1;i<img_input.rows-1;i++){
for (int j=1;j<img_input.cols-1;j++){
la2 = 8*img_input.at<uchar>(i,j)-img_input.at<uchar>(i-1,j)-img_input.at<uchar>(i+1,j)-
img_input.at<uchar>(i,j-1)-img_input.at<uchar>(i,j+1)-img_input.at<uchar>(i-1,j-1)-
img_input.at<uchar>(i-1,j+1)-img_input.at<uchar>(i+1,j-1)-img_input.at<uchar>(i+1,j+1);

img_output.at<uchar>(i,j) = saturate_cast<uchar>(la2+img_output.at<uchar>(i,j));
}
}
}